//
//  ViewController.m
//  XXscroll
//
//  Created by h92 on 14/12/15.
//  Copyright (c) 2014年 李腾飞. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize scrollView;
@synthesize textfield;

- (void)viewDidLoad {
    [super viewDidLoad];

    // 重绘脚的大小
    self.scrollView.contentSize=CGSizeMake(320, 900);
    
    // 可怜的老板自己干活
    self.textfield.delegate=self;
    
}

# pragma mark -学生（系统） 在监考老师（controler）处报名 报名号码：UIKeyboardDidShowNotification

-(void) viewDidAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:)
                                                name:UIKeyboardDidHideNotification object:nil];
}

# pragma mark -学生（系统） 在监考老师（controler）处报名 取消报名号码：UIKeyboardDidShowNotification

-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIKeyboardDidHideNotification object:nil];
}

-(void) keyboardDidShow: (NSNotification *)notif {
    
    if (keyboardVision) {//键盘已经出现要忽略通知
        return;
    }
    // 获得键盘尺寸
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    //重新定义ScrollView的尺寸
    CGRect viewFrame = self.scrollView.frame;
    viewFrame.size.height -= (keyboardSize.height);
    self.scrollView.frame = viewFrame;
    
    //滚动到当前文本框
    CGRect textFieldRect = [self.textfield frame];
    [self.scrollView scrollRectToVisible:textFieldRect animated:YES];
    
    keyboardVision = YES;
}

-(void) keyboardDidHide: (NSNotification *)notif {
    
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    CGRect viewFrame = self.scrollView.frame;
    viewFrame.size.height += keyboardSize.height;
    self.scrollView.frame = viewFrame;
    
    if (!keyboardVision) {
        return;
    }
    
    keyboardVision = NO;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
